using System.Diagnostics;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using YesPlayLove.Classes;
using jQueryApi;

namespace YesPlayLove.Clusters
{
    [IgnoreNamespace]
    public abstract class EmailBase : Truple
    {
        internal bool Active;
        internal EmailBubbles EmailBubbles;
        internal EmailCluster EmailCluster;
        internal EmailInbox EmailInbox;
        internal EmailProfile EmailProfile;
        protected static EmailData Data;
        protected jQueryObject Wrapper;

        public EmailBase(EmailCluster cluster)
        {
            if (Data == null)
                Data = new EmailData();
            EmailCluster = cluster;
        }

        public void Initialize(bool active)
        {
            Active = active;
            Debug.Assert(Element != null, "Element != null");
            // ReSharper disable PossibleNullReferenceException
            Element.Empty();
            // ReSharper restore PossibleNullReferenceException
        }

        public abstract EmailBase Build(bool active);
        public abstract EmailBase Fill(object item);

        public virtual EmailBase FocusInput()
        {
            return this;
        }

        public void BaseRebuild(bool active)
        {
            new Await()
                .AddAw(FadeOutAw, ThemeBase.FadeOut)
                .AddDx(delegate { Build(active); })
                .AddAw(FadeInAw, ThemeBase.FadeIn)
                .Commit();
        }

        public void ScrollInActiveAsync()
        {
            if (Active)
                new Await().AddAw(ScrollInViewAw, Element).Commit();
        }

        internal EmailBase HookupCluster(EmailCluster cluster)
        {
            Debug.Assert(cluster != null, "cluster != null");
            // ReSharper disable PossibleNullReferenceException
            EmailInbox = cluster.EmailInbox;
            // ReSharper restore PossibleNullReferenceException
            EmailProfile = cluster.EmailProfile;
            EmailBubbles = cluster.EmailBubbles;
            return this;
        }

        internal EmailBase HookupAll(EmailCluster cluster)
        {
            EmailInbox.HookupCluster(cluster);
            EmailProfile.HookupCluster(cluster);
            EmailBubbles.HookupCluster(cluster);
            return this;
        }
    }
}